package com.changgou.goods.controller;

import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/***
 * 描述
 * @author ljh
 * @packagename com.changgou.goods.controller
 * @version 1.0
 * @date 2020/5/5
 */
@RestController//@resonsebody + @controller 组合
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;



    //查询所有的品牌的列表
    //@RequestMapping(value="/findAll",method = RequestMethod.GET)=  @GetMapping("/findAll")
    @GetMapping
    public Result<List<Brand>> findAll() {
        //调用service 获取所有的品牌列表数据
        List<Brand> brandList = brandService.findAll();
        return new Result<List<Brand>>(true, StatusCode.OK,"查询品牌列表成功",brandList);
    }


    /**
     * 根据品牌的ID 获取品牌的数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable(name="id") Integer id){
        Brand brand = brandService.findById(id);
        return new Result<Brand>(true, StatusCode.OK,"根据ID查询品牌成功",brand);
    }

    /**
     *  更新数据
     * @param brand  用于接收页面传递过来的表单的数据JSON
     * @param id 传递过来的品牌的iD主键值
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@RequestBody Brand brand,@PathVariable(name="id")Integer id){
        brand.setId(id);
        brandService.update(brand);
        return new Result(true, StatusCode.OK,"更新成功");
    }

}
